<?php
/**
 * Psa_Exception class.
 * 
 * LICENSE:
 * 
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with This library. If not, see <{@link http://www.gnu.org/licenses/}>.
 *
 * @link http://code.google.com/p/phpstartapp/
 * @author Bojan Mauser <bmauser@gmail.com>
 * @copyright Bojan Mauser 2009
 * @package psa
 * @subpackage exceptions
 * @version $Id: $
 */


/**
 * Psa_Exception class.
 * This class extends PHP Exception class and adds {@link log()} method 
 * for writing PSA log. Other PSA exception classes extend this class.
 * You can also extend this class for your custom exceptions. If logs are enabled
 * in {@link config.php} file every exception message will be logged.
 */
class Psa_Exception extends Exception{

	
	/**
	 * Constructor.
	 * 
	 * Calls {@link log()} method which writes message to log.
	 * 
	 * @param string|array $message exception message or associative 
	 * array with values for {@link log()} method.
	 * @param string $code user defined exception code
	 * @param bool $write_log set to false if you don't want to write exception to log
	 */
	public function __construct($message = '', $code = 0, $write_log = true){

		if(is_array($message))
			$exception_message = $message['message'];
		else
			$exception_message = $message;
			
		// make sure everything is assigned properly
		parent::__construct($exception_message, (int)$code); //$code can be string here
		
		// write log
		if($write_log)
			$this->log($message);
	}

	
	/**
	 * Writes a log message.
	 * 
	 * @param string|array $log_message message to be logged or associative 
	 * array with values for {@link Psa_Logger::log()} method. If you pass 
	 * array some elements, if not set, will get default values. 
	 * Default values are: 
	 * - for <kbd>'function'</kbd> element is return from <kbd>getTraceAsString()</kbd>
	 *   exception method
	 * - for <kbd>'level'</kbd> element is 1
	 * - for <kbd>'type'</kbd> element is <kbd>'exception'</kbd>.
	 * 
	 * @see Psa_Logger::log()
	 */
	protected function log($log_message){

		global $PSA_CFG;
		
		// if logging is enabled
		if($PSA_CFG['logging']['max_log_level'] >= 1){
			// parameters for Psa_Logger::log() method
			if(is_array($log_message)){
				$log_data = &$log_message;
			}
			else
				$log_data['message'] = $log_message;
			
			if(!@$log_data['function'])
				$log_data['function'] = $this->getTraceAsString(); //psa_get_backtrace();
			
			if(!@$log_data['level'])
				$log_data['level'] = 1;
			
			if(!@$log_data['type'])
				$log_data['type'] = 'exception';
			
			Psa_Logger::get_instance()->log($log_data);
		}
	}
}

